from flask import Flask, request, Response, make_response
import time

app = Flask(__name__)

# 根路由
@app.route('/')
def hello_world():
    return 'Hello, World!'

# 设置 Cookie
@app.route('/add')
def add_cookie():
    res = Response('Cookie added')
    res.set_cookie(key='name', value='letian', expires=time.time() + 6 * 60)  # 设置 Cookie，6 分钟后过期
    return res

# 获取 Cookie
@app.route('/show')
def show_cookie():
    cookies = request.cookies
    if 'name' in cookies:
        return f"Cookie 'name' is: {cookies['name']}"
    else:
        return "Cookie 'name' not found"

# 删除 Cookie
@app.route('/del')
def del_cookie():
    res = Response('Cookie deleted')
    res.set_cookie(key='name', value='', expires=0)  # 删除 Cookie
    return res

if __name__ == '__main__':
    # 启动http服务器 接受http请求
    app.run(port=5000, debug=True)